<?php

class Nld_Models
{
	protected static $_tables = array();
	
	static function quoteInto($text, $value, $type = null, $count = null)
	{
		return self::getAdapter()->quoteInto($text, $value, $type, $count);
	}
	static function select()
	{
		return Zend_Db_Table::getDefaultAdapter()->select();
	}
	static function getAdapter()
	{
		return Zend_Db_Table::getDefaultAdapter();
	}
	static function getTableClass($key)
	{
		return 'Nld_Models_'.str_replace('/', '_', $key).'_DbTable';
	}
	static function getTable($key, $options = null, $new = false)
	{
		$object = null;
		$class = self::getTableClass($key);
		
		if($new)
		{
			if($options === null)
			{
				$object = new $class;
			}
			else
			{
				$object = new $class($options);	
			}
		}
		else
		{
			if(empty(self::$_tables['table_'.$key]))
			{
				if($options === null)
				{
					$object = new $class;
				}
				else
				{
					$object = new $class($options);	
				}
				self::$_tables['table_'.$key] = $object;
			}
			$object = self::$_tables['table_'.$key];
		}
			
		return $object;	
	}
	
	static function getRow($key, $options = null, $new = false)
	{
		$object = null;
		$class = 'Nld_Models_'.str_replace('/', '_', $key).'_Row';
		
		if($new)
		{
			if($options === null)
			{
				$object = new $class;
			}
			else
			{
				$object = new $class($options);	
			}
		}
		else
		{
			if(empty(self::$_tables['row_'.$key]))
			{
				if($options === null)
				{
					$object = new $class;
				}
				else
				{
					$object = new $class($options);	
				}
				self::$_tables['row_'.$key] = $object;
			}
			$object = self::$_tables['row_'.$key];
		}
			
		return $object;	
	}
	
	public function pagging($page = 1, $perpage = 20, $display = 5, $url = '', $select = null, $cache = true)
	{
		$entries	=	array();
		if($select instanceof Zend_Db_Table)
			return array();

		$perpage 	= (intval($perpage) < 1)?1:intval($perpage);
		$display 	= (intval($display) < 1)?1:intval($display);
		$total 		= 0;
		
		if(is_array($select))
		{
			$total 	= count($select);
		}
		else
		{
			$tmp_total 	= ($cache)
											?Nld_Cache::query(self::select()->from(array('aa' => $select), 'COUNT(*) AS t'), 'cache/pagging/')
											:self::select()->from(array('aa' => $select), 'COUNT(*) AS t')->query()->fetchAll();
			$total 			= intval(@$tmp_total[0]['t']);
		}

		$totalpages = ceil($total/$perpage);
		
		$page 		= (intval($page) > $totalpages)?$totalpages:intval($page);
		$page 		= (intval($page) < 1)?1:intval($page);

		if($totalpages <= $display)
		{
			$begin 	= 1;
			$end 		= $totalpages;
		}
		elseif($page <= ($display/2))
		{
			$begin 	= 1;
			$end 		= $display;
		}
		elseif($page >= ($totalpages-intval($display/2)))
		{
			$begin 	= $totalpages-$display+1;
			$end 		= $totalpages;
		}
		{
			$begin 	= $page-intval($display/2);
			$end 		= $begin+$display-1;
		}
		
		if(is_array($select))
		{
			$entries 	= array_slice($select, ($page-1)*$perpage, $perpage);
		}
		else
		{
			$select->limit($perpage,($page-1)*$perpage);
			$entries 	= ($cache)?Nld_Cache::query($select, 'cache/pagging/'):$select->query()->fetchAll();
		}

		return array(
			'perpage'				=>	$perpage,
			'display'				=>	$display,
			'total'					=>	$total,
			'totalpages'		=>	$totalpages,
			'page'					=>	$page,
			'begin'					=>	$begin,
			'end'						=>	$end,
			'url'						=>	$url,
			'entries'				=>	$entries
		);
	}

}